<template>
  <div class="_wz_cent_main _wz_succession">
    <Card dis-hover :bordered="true">
      <p slot="title">列表</p>
      <p slot="extra">
        <Button type="primary" @click="addData">添加</Button>
      </p>
      <wzTbale ref="wzTbale" :tableData="tableData"></wzTbale>
    </Card>


    <Modal v-model="addDataType" :title="formTitle" :mask-closable="false" :footer-hide="true" width="400">
      <div v-if="addDataType">
        <Form ref="formAdd" :model="formData" label-position="right" :label-width="120" :rules="ruleValidate" class="_wz_succession_add">
          <FormItem label="驾驶时间" prop="dateRange">
            <TimePicker format="HH:mm" type="timerange" placement="bottom-end" v-model="formData.dateRange" placeholder="驾驶时间" @on-change="handleChange"></TimePicker>
          </FormItem>
          <FormItem label="连续时间" prop="continuousTime">
            <i-input v-model="formData.continuousTime" placeholder="连续行驶时间" clearable />
          </FormItem>
          <FormItem label="休息地点" prop="breakPoint">
            <i-input v-model="formData.breakPoint" placeholder="换驾、休息地点" clearable />
          </FormItem>
          <FormItem label="交班人" prop="shifter">
            <i-input v-model="formData.shifter" placeholder="交班人" clearable />
          </FormItem>
          <FormItem label="接班人" prop="successor">
            <i-input v-model="formData.successor" placeholder="接班人" clearable />
          </FormItem>
          <FormItem label="接班图片">
            <Upload :before-upload="shiftImgFileFun" action="" style="color: #2d8cf0;cursor: pointer;">上传接班图片照片
            </Upload>
            <div class="demo-upload-list">
              <viewer style="width: 100%;height: 100%;">
                <img :src="!!shiftImgFile.url?shiftImgFile.url:'../../static/img/zwtpxd.png'" @error="public.imgDispose">
              </viewer>
            </div>
          </FormItem>
          <div class="demo-drawer-footer">
            <Button type="primary" @click="handleSubmit('formAdd')">提交</Button>
          </div>
        </Form>
      </div>

    </Modal>

  </div>
</template>
<script>
import wzTbale from "../../../components/table/table";

export default {
  components: {
    wzTbale,//列表
  },
  props: ['selectData'],//接收来自父组件的数据
  data() {
    return {
      uploadProgress: 0,//文件上传进度
      ruleValidate: {
        dateRange: [
          {
            type: 'array',required: true,message: '请选择驾驶时间',
            fields: {
              0: {required: true, message: '请输入起止日期'},
              1: {required: true, message: '请输入起止日期'}
            }
          }
        ],
        continuousTime: [
          {required: true, message: '请填写连续行驶时间', trigger: 'blur'},
        ],
        breakPoint: [
          {required: true, message: '请填写换驾、休息地点', trigger: 'blur'},
        ],
        shifter: [
          {required: true, message: '请填写交班人', trigger: 'blur'},
        ],
        successor: [
          {required: true, message: '请填写接班人', trigger: 'blur'},
        ],
      },
      formTitle: '',
      formData: {
        beginDrive: '', endDrive: '', continuousTime: '', breakPoint: '', shifter: '', successor: '', dateRange: []
      },
      shiftImgFile: {//行车中接班图片照片
        data: null,
        url: '',
      },
      addDataType: false,
      tableData: {
        url: this.apiUrl.webApi + "/safety/drivingLog/drivingLogShiftList",//请求地址
        method: 'post',//请求方式
        where: {uuid: this.selectData.uuid},//搜索条件
        height: 'auto',//表格高度 auto
        page: false,//分页是否使用
        columns: [//表头数据
          {type: 'index', width: 70, align: 'center', title: '序号'},
          {
            title: '驾驶时间', minWidth: 120, key: 'beginDrive', render: (h, params) => {
              return h('span', params.row.beginDrive + ' - ' + params.row.endDrive);
            }
          },
          {title: '行驶时长', minWidth: 100, key: 'continuousTime'},
          {title: '换驾、休息地点', minWidth: 160, key: 'breakPoint'},
          {title: '交班人', minWidth: 100, key: 'shifter'},
          {title: '接班人', minWidth: 100, key: 'successor'},
          {
            title: '操作', fixed: 'right', width: 120,
            render: (h, params) => {
              let operationButtons = [];
              operationButtons.push(h('span', {
                style: {"color": "#2d8cf0", "cursor": "pointer"},
                on: {
                  click: () => {
                    this.formTitle = '修改';
                    var data = JSON.parse(JSON.stringify(params.row));
                    data.dateRange = [data.beginDrive, data.endDrive]
                    this.shiftImgFile.url = !!data.shiftImg ? this.apiUrl.imgUrl + data.shiftImg : '';
                    this.formData = data;
                    this.addDataType = true;
                  }
                }
              }, '修改'))
              operationButtons.push(h('Divider', {props: {type: 'vertical',}}, ''))
              operationButtons.push(h('span', {
                style: {"color": "#2d8cf0", "cursor": "pointer"},
                on: {
                  click: () => {
                    var that = this;
                    that.$Modal.confirm({
                      title: '删除提醒',
                      content: '确定要删除选中的记录？',
                      onOk: () => {
                        that.spinShow = true;
                        that.axios.post(that.apiUrl.webApi + "/safety/drivingLog/drivingLogShiftDelete", {uuid: params.row.uuid}).then(res => {
                          that.spinShow = false;
                          if (!!res) {
                            that.$Message.success('删除成功');
                            that.$refs.wzTbale.tableLoadData();
                          }
                        }).catch(err => {
                          console.log("失败", err)
                        })
                      },
                      onCancel: () => {
                        //点击取消
                      }
                    });
                  }
                }
              }, '删除'))
              return operationButtons;
            }
          }
        ],
        format: {//返回数据格式
          list: ['data'],//数据list
          count: ['count'],//数据总量
        },
      },
    }
  },
  computed: {//计算属性

  },
  beforeCreate() {//beforeCreate创建前状态

  },
  created() {//created创建完毕状态

  },
  beforeMount() {//beforeMount挂载前状态   DOM 元素然后塞入页面的过程

  },
  mounted() {//mounted 挂载结束状态

  },
  methods: {//执行的方法
    //交班记录 时间范围
    handleChange(date) {
      var that = this;
      if (!!date[0]) {
        that.formData.beginDrive = date[0];
        that.formData.endDrive = date[1];
        var begin = new Date(this.public.timeFormatting(new Date(), "yyyy-MM-dd") + ' ' + date[0] + ':00').getTime();
        var over = new Date(this.public.timeFormatting(new Date(), "yyyy-MM-dd") + ' ' + date[1] + ':00').getTime();
        var difference = parseInt(over) - parseInt(begin);
        that.formData.continuousTime = parseInt(difference / 1000 / 60) + '';
      } else {
        that.formData.beginDrive = '';
        that.formData.endDrive = '';
        that.formData.continuousTime = '';
      }
    },
    addData() {
      var that = this;
      that.shiftImgFile = {//行车中接班图片照片
        data: null,
        url: '',
      };
      that.formData = {
        beginDrive: '',
        endDrive: '',
        continuousTime: '',
        breakPoint: '',
        shifter: '',
        successor: '',
        dateRange: []
      };
      that.formTitle = '添加';
      that.addDataType = true;
    },
    //提交
    handleSubmit(name) {
      var that = this;

      that.$refs[name].validate((valid) => {
        if (valid) {//正则判断使用
          var data = that.formData;
          that.formData.drivingLogUuid = that.selectData.uuid
          let formData = new FormData();
          for (let key in data) {
            if (!!data[key]) {
              formData.append(key, data[key]);
            }
          }

          if (!!that.shiftImgFile.data) {//
            formData.append('shiftImgFile', that.shiftImgFile.data);
          }

          that.$Spin.show();//显示加载
          that.axios.post(that.apiUrl.webApi + '/safety/drivingLog/drivingLogShiftSaveOrUpdate', formData, that.public.parameterUploadProgress(that)).then(res => {
            that.$Spin.hide();
            if (res) {
              that.$Message.success('操作成功');
              that.addDataType = false;
              that.$refs.wzTbale.tableLoadData(true);
            }
          }).catch(err => {
            console.log("失败", err)
          })
        } else {
          this.$Message.error('请正确填写表单')
        }
      })
    },

    shiftImgFileFun(file) {//行车中接班图片照片
      let that = this;
      let type = file.type.split("/")[0];
      if (type == "image") {//图片
        that.shiftImgFile.url = window.URL.createObjectURL(file);
        that.shiftImgFile.data = file;
      } else {
        that.$Message.warning('请选择jpg、png、jpeg的图片进行上传');
      }
      return false;
    },

  },
  watch: {//监听

  },
  beforeUpdate() {//beforeUpdate 更新前状态

  },
  updated() {//updated 更新完成状态

  },
  beforeDestroy() {//beforeDestroy 销毁前状态

  },
  destroyed() {//destroyed 销毁完成状态

  }
}
</script>
<style lang="less">

._wz_succession_add {
  padding: 0 0 53px 0;

  .demo-upload-list {
    display: inline-block;
    width: 200px;
    height: 140px;
    text-align: center;
    line-height: 140px;
    border-radius: 4px;
    overflow: hidden;
    background: #fff;
    position: relative;
    border: 1px solid rgba(0, 0, 0, .2);
    margin-right: 4px;

    img {
      width: 100%;
      height: 100%;
    }
  }
}
</style>
